// pages/denglu/denglu.js
const {denglu,getOpenId,scan} =require('../../util/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token:"",
    phone:"",
    memberId:"",
    nickName:"",
    avatarUrl:"",
    encryptedData:"",
    sessionKey:'',
    iv:"",
    code:"",
    openId:'',
    show:false,
    abc:true

    

  },
  shouquan(){
    if(this.data.avatarUrl == '')
    {
      wx.showToast({
        title: '请选择头像',
        icon:'none'
      })
    }
    else if(this.data.nickName == '')
    {
      wx.showToast({
        title: '请输入昵称',
        icon:'none'
      })
    }else
    {
     this.setData({
       abc:false,
       shows:false,
       onshow:false,
       icon:this.data.avatarUrl
     })
    
     denglu(this.data).then(res=>{
     
       
       this.setData({

         phone:res.aaMember.memberPhone
       })
       if(res.code==200){
      
      wx.setStorageSync('phone',this.data.phone)
      wx.setStorageSync('memberId', res.aaMember.id)
      wx.setStorageSync('onshow', false)
       wx.setStorageSync('token', res.token)
       wx.setStorageSync('memberName', res.aaMember.memberName)
       wx.setStorageSync('authentication', res.aaMember.authentication)
       wx.setStorageSync('avatarUrl', this.data.avatarUrl)
       console.log("登录成功",wx.getStorageSync('tuiguangId'))
       if(wx.getStorageSync('tuiguangId')!=''){
         let p={
           memberId:wx.getStorageSync('memberId'),
           userId:wx.getStorageSync('tuiguangId')
         }
         scan(p).then(res=>{
             if(res.code==200){
               console.log("绑定成功")
             }
         })
       }
       this.onLoad()
       wx.navigateBack({
         delta: 1,
       })
       
       }else{
         wx.showToast({
           title: res.msg,
           icon: 'error',
           duration: 2000
         })
       }     
     })        
    }
    
  },
  onChooseAvatar(e){
   
    this.setData({
     avatarUrl:e.detail.avatarUrl,
    })
  },
  login() {
    
  
 },

 bindnicknamereview(e){
   this.setData({
    nickName: e.detail.value.name
   })
  wx.setStorageSync('nickName', this.data.nickName)

  wx.setStorageSync('avatarUrl', this.data.avatarUrl)
  
  

  this.setData({
    show:true
  })
   
 },

  getPhoneNumber (e) {
   this.setData({
     abc:false,
    show: true,
    iv:e.detail.iv,
    encryptedData:e.detail.encryptedData,
    code:e.detail.code

   })
   this.shouquan()
 },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.login({
      success:(res)=>{
       
           let params={
             code:res.code
            };    
        getOpenId(params).then(res=>{
          this.setData({
            sessionKey:res.data.sessionKey,
            openId:res.data.openId
          })
         
        }) 
      }
    })  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      phone:wx.getStorageSync('phone'),
      avatarUrl:wx.getStorageSync('avatarUrl'),
      onshow:wx.getStorageSync('onshow'),
      nickName:wx.getStorageSync("nickName")
    })
   

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})